import numpy as np
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score, precision_score, recall_score, f1_score

# load data
path = r'../../../../../large_data/hand_writing/'
data = np.loadtxt(path + 'imgX.txt', delimiter=',')
y = np.loadtxt(path + 'labely.txt', delimiter=',')
m = len(y)
y[y == 10] = 0  # ATTENTION

# scale data
mu = data.mean()
sigma = data.std()
data -= mu
data /= sigma

# shuffle data
rnd_idx = np.random.permutation(m)
data = data[rnd_idx]
y = y[rnd_idx]
x = data

# splice data
XX = np.c_[np.ones(m), data]

# onehot
y_onehot = np.zeros([m, 10])
for k, v in enumerate(y):
    y_onehot[k, int(v)] = 1
# print(y[:10])
# print(y_onehot[:10])

# split data
m_train = int(0.7 * m)
m_test = m - m_train
x_train, x_test = np.split(x, [m_train])
y_train, y_test = np.split(y, [m_train])
XX_train, XX_test = np.split(XX, [m_train])
y_onehot_train, y_onehot_test = np.split(y_onehot, [m_train])

model = MLPClassifier(hidden_layer_sizes=[100, 64], max_iter=300)
model.fit(x_train, y_train)
print(f'score = {model.score(x_test, y_test)}')

print('weight', model.coefs_)
print('intercept', model.intercepts_)
print('iteration', model.n_iter_)
print('感知机layer num', model.n_layers_)
print('output unit num', model.n_outputs_)
print('act func', model.out_activation_)

h = model.predict(x)
print('confusion', confusion_matrix(y, h))
print('rpt', classification_report(y, h))
print('acc', accuracy_score(y, h))
print('precision', precision_score(y, h, average='micro'))
print('recall', recall_score(y, h, average='micro'))
print('f1', f1_score(y, h, average='micro'))
